package bat.iog.hytool.reactor.multiple;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

class ReadHandler{

    private SelectionKey selectionKey;

    public ReadHandler(SelectionKey selectionKey) {
        this.selectionKey = selectionKey;
    }


    public void run() {
        try {
            SocketChannel sc = (SocketChannel) selectionKey.channel();
            //处理读请求
            doRead(sc);
            //处理完读请求，将通道注册为写
            Selector selector = selectionKey.selector();
            SelectionKey sk = sc.register(selector, SelectionKey.OP_WRITE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void doRead(SocketChannel ssc) {
        System.out.println("读取数据，然后做一些数据处理");
    }
}
